using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using FarPoint.Win.Spread.DrawingSpace;
using FarPoint.Win.Spread;

namespace Spread.FloatingObjects.Features
{
    public partial class ShapeEvents : Spread.Common.Features.Sample.BaseSampleControl
    {
        public ShapeEvents()
        {
            InitializeComponent();
            Name = "Shape Events";

            InitSpreadSheet(fpSpread.ActiveSheet);
            InitControls();

            fpSpread.ShapeActivated += FpSpread_ShapeActivated;
            fpSpread.ShapeDeactivated += FpSpread_ShapeDeactivated;
        }

        #region Private methods
        private void InitSpreadSheet(SheetView sheet)
        {
            MultiSideShape hexagon = new MultiSideShape();
            hexagon.Sides = 6;
            hexagon.Size = new Size(100, 100);
            hexagon.Name = "Hexagon";
            hexagon.BackColor = Color.DodgerBlue;
            hexagon.ShapeOutlineStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            hexagon.PropertyChanged += Shape_PropertyChanged;
            hexagon.MouseLeave += Shape_MouseLeave;
            hexagon.MouseEnter += Shape_MouseEnter;
            hexagon.Click += Shape_Click;
            hexagon.DoubleClick += Shape_DoubleClick;

            MultiSideShape dodecagon = new MultiSideShape();
            dodecagon.Sides = 12;
            dodecagon.Size = new Size(100, 100);
            dodecagon.Name = "Dodecagon";
            dodecagon.BackColor = Color.LightCyan;
            dodecagon.ShapeOutlineStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            dodecagon.PropertyChanged += Shape_PropertyChanged;
            dodecagon.MouseLeave += Shape_MouseLeave;
            dodecagon.MouseEnter += Shape_MouseEnter;
            dodecagon.Click += Shape_Click;
            dodecagon.DoubleClick += Shape_DoubleClick;

            sheet.AddShape(hexagon, 4, 3);
            sheet.AddShape(dodecagon, 4, 8);
        }

        private void InitControls()
        {
            comboImage.Items.AddRange(new string[] { "Picture", "Background Image", "Foreground Image" });
            listBox.Items.Clear();

            btnClear.Click += BtnClear_Click;
            comboImage.SelectedIndexChanged += ComboImage_SelectedIndexChanged;
            btnSetPicture.Click += BtnSetPicture_Click;
        }

        private Image GetImageUsingFileDialog()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Multiselect = false;
            dialog.Filter = "Images|*.jpeg;*.jpg;*.png;*.bmp";

            Image image = null;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    image = Image.FromFile(dialog.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error loading image", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            return image;
        }

        private void ToggleControls(bool enable)
        {
            comboImage.Enabled = enable;
            if (!enable)
                comboImage.SelectedIndex = -1;
        }
        #endregion

        #region Event handlers

        private void FpSpread_ShapeDeactivated(object sender, EventArgs e)
        {
            ToggleControls(false);
        }

        private void FpSpread_ShapeActivated(object sender, EventArgs e)
        {
            ToggleControls(true);
            PSShape shape = (fpSpread.ActiveWindowlessObject as PSShape);
            listBox.Items.Add($"ShapeActivated: {shape.Name}");
        }

        private void BtnSetPicture_Click(object sender, EventArgs e)
        {
            Image image = GetImageUsingFileDialog();
            if (image == null)
                return;

            PSShape shape = fpSpread.ActiveWindowlessObject as PSShape;
            switch (comboImage.SelectedItem.ToString())
            {
                case "Picture":
                    shape.Picture = image;
                    break;
                case "Background Image":
                    shape.BackgroundImage = new FarPoint.Win.Picture(image, FarPoint.Win.RenderStyle.StretchAndScale);
                    break;
                case "Foreground Image":
                    shape.ForegroundImage = new FarPoint.Win.Picture(image, FarPoint.Win.RenderStyle.StretchAndScale);
                    break;
            }
        }

        private void ComboImage_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSetPicture.Enabled = (comboImage.SelectedIndex != -1);
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            listBox.Items.Clear();
        }

        #region Shape Events
        private void Shape_DoubleClick(object sender, EventArgs e)
        {
            listBox.Items.Add($"DoubleClick on shape '{(sender as PSShape).Name}'");
        }

        private void Shape_Click(object sender, EventArgs e)
        {
            listBox.Items.Add($"Clicked on shape '{(sender as PSShape).Name}'");
        }

        private void Shape_MouseEnter(object sender, MouseEventArgs e)
        {
            listBox.Items.Add($"MouseEnter for shape '{(sender as PSShape).Name}'");
        }

        private void Shape_MouseLeave(object sender, MouseEventArgs e)
        {
            listBox.Items.Add($"MouseLeave for shape '{(sender as PSShape).Name}'");
        }

        private void Shape_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            listBox.Items.Add($"Property '{e.PropertyName}' changed for shape '{(sender as PSShape).Name}'");
        }
        #endregion

        #endregion
    }
}


//
//ShapeEvents.Designer.cs
//


namespace Spread.FloatingObjects.Features
{
    partial class ShapeEvents
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ShapeEvents));
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.listBox = new System.Windows.Forms.ListBox();
            this.btnClear = new System.Windows.Forms.Button();
            this.comboImage = new System.Windows.Forms.ComboBox();
            this.btnSetPicture = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox2);
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White;
            this.fpSpread.StatusBarVisible = true;
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.btnClear);
            this.groupBox1.Controls.Add(this.listBox);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(157, 204);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Events";
            // 
            // listBox
            // 
            this.listBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.listBox.FormattingEnabled = true;
            this.listBox.Location = new System.Drawing.Point(6, 19);
            this.listBox.Name = "listBox";
            this.listBox.Size = new System.Drawing.Size(145, 134);
            this.listBox.TabIndex = 0;
            // 
            // btnClear
            // 
            this.btnClear.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnClear.Location = new System.Drawing.Point(29, 161);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(100, 25);
            this.btnClear.TabIndex = 1;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            // 
            // comboImage
            // 
            this.comboImage.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboImage.Enabled = false;
            this.comboImage.FormattingEnabled = true;
            this.comboImage.Location = new System.Drawing.Point(81, 39);
            this.comboImage.Name = "comboImage";
            this.comboImage.Size = new System.Drawing.Size(70, 21);
            this.comboImage.TabIndex = 1;
            // 
            // btnSetPicture
            // 
            this.btnSetPicture.Enabled = false;
            this.btnSetPicture.Image = ((System.Drawing.Image)(resources.GetObject("btnSetPicture.Image")));
            this.btnSetPicture.Location = new System.Drawing.Point(6, 19);
            this.btnSetPicture.Name = "btnSetPicture";
            this.btnSetPicture.Size = new System.Drawing.Size(69, 59);
            this.btnSetPicture.TabIndex = 9;
            this.btnSetPicture.Text = "Set Picture";
            this.btnSetPicture.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.btnSetPicture.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.btnSetPicture.UseVisualStyleBackColor = false;
            // 
            // groupBox2
            // 
            this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox2.Controls.Add(this.btnSetPicture);
            this.groupBox2.Controls.Add(this.comboImage);
            this.groupBox2.Location = new System.Drawing.Point(0, 210);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(157, 89);
            this.groupBox2.TabIndex = 10;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Set Image";
            // 
            // ShapeEvents
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "ShapeEvents";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.ListBox listBox;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.ComboBox comboImage;
        private System.Windows.Forms.Button btnSetPicture;
        private System.Windows.Forms.GroupBox groupBox2;
    }
}
